<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="内部类别" prop="categId">
        <el-select v-model="queryParams.categId" placeholder="请选择内部类别" clearable>
          <el-option v-for="dict in dict.type.product_category" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
        <el-form-item>

          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      
  
      <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange" @row-click="Addprice">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="库存类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.product_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="内部类别" align="center" prop="categId">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.product_category" :value="scope.row.categId" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="序列" align="center" prop="sequence" /> -->
        <!-- <el-table-column label="产品描述" align="center" prop="description" />
        <el-table-column label="采购说明" align="center" prop="descriptionPurchase" />
        <el-table-column label="销售描述" align="center" prop="descriptionSale" /> -->
  
  
        <!-- <el-table-column label="售价" align="center" prop="listPrice" />
        <el-table-column label="体积(kg)" align="center" prop="volume" />
        <el-table-column label="重量(m³)" align="center" prop="weight" />
        <el-table-column label="可用于销售" align="center" prop="saleOk" />
        <el-table-column label="可用于购买" align="center" prop="purchaseOk" />
        <el-table-column label="Unit of Measure" align="center" prop="uomId" />
        <el-table-column label="Active" align="center" prop="active" />
        <el-table-column label="颜色" align="center" prop="color" />
        <el-table-column label="创建者id" align="center" prop="createUid" /> -->
        <el-table-column label="创建日期" align="center" prop="createDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="最后更新者id" align="center" prop="writeUid" /> -->
        <el-table-column label="最后更新日期" align="center" prop="writeDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
  
        <!-- <el-table-column label="可用于费用" align="center" prop="canBeExpensed" /> -->
        <!-- <el-table-column label="仓库(海内外)" align="center" prop="warehouse" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" @click.stop>
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" icon="el-icon-document" @click="detail(scope.row)"
              v-hasPermi="['system:stock:edit']">详情</el-button> -->
            <!-- <el-button size="mini" type="text" icon="el-icon-document" @click="Operationdetail(scope.row)"
              v-hasPermi="['system:stock:edit']">操作详情</el-button> -->
            <el-button size="mini" type="text" icon="el-icon-c-scale-to-original" @click.stop="handleUpdate(scope.row)"
              >添加供应商报价</el-button>
            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:stock:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
  
      <!-- 添加或修改供应商报价对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <!-- <el-form-item label="id" prop="id">
            <el-input v-model="form.id" placeholder="请输入id" />
          </el-form-item> -->
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称" />
          </el-form-item>
          <!-- <el-form-item label="规格" prop="specification">
            <el-input v-model="form.specification"  placeholder="请输入规格" />
          </el-form-item> -->
          <!-- <el-form-item label="单位" prop="uomId">
            <el-input v-model="form.uomId" placeholder="请输入单位" />
          </el-form-item> -->
          <!-- <el-form-item label="序列" prop="sequence">
            <el-input v-model="form.sequence" placeholder="请输入序列" />
          </el-form-item> -->
          <!-- <el-form-item label="产品描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入库存描述" />
          </el-form-item> -->
          <!-- <el-form-item label="采购说明" prop="descriptionPurchase">
            <el-input v-model="form.descriptionPurchase" placeholder="请输入采购描述" />
          </el-form-item> -->
          <!-- <el-form-item label="销售描述" prop="descriptionSale">
            <el-input v-model="form.descriptionSale" placeholder="请输入销售描述" />
          </el-form-item> -->
          <!-- <el-form-item label="库存类型" prop="type">
            <el-input v-model="form.type" placeholder="请输入库存类型" /> -->
            <!-- <el-select v-model="form.type" placeholder="请选择类型">
              <el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select> -->
          <!-- </el-form-item> -->
          <!-- <el-form-item label="内部类别" prop="categId">
            <el-select v-model="form.categId" placeholder="请选择内部类别">
              <el-option v-for="dict in dict.type.product_category" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="售价" prop="listPrice">
            <el-input v-model="form.listPrice" placeholder="请输入售价" />
          </el-form-item> -->
          <!-- <el-form-item label="体积(kg)" prop="volume">
            <el-input v-model="form.volume" placeholder="请输入体积" />
          </el-form-item>
          <el-form-item label="重量(m³)" prop="weight">
            <el-input v-model="form.weight" placeholder="请输入重量" />
          </el-form-item> -->
          <!-- <el-form-item label="可用于销售" prop="saleOk"> -->
            <!-- <el-input v-model="form.saleOk" placeholder="请输入可用于销售" /> -->
            <!-- <el-radio v-model="form.saleOk" label="t">是</el-radio>
            <el-radio v-model="form.saleOk" label="f">否</el-radio>
          </el-form-item> -->
          <!-- <el-form-item label="可用于购买" prop="purchaseOk"> -->
            <!-- <el-input v-model="form.purchaseOk" placeholder="请输入可用于购买" /> -->
            <!-- <el-radio v-model="form.purchaseOk" label="t">是</el-radio>
            <el-radio v-model="form.purchaseOk" label="f">否</el-radio>
          </el-form-item>
          <el-form-item label="可用于费用" prop="canBeExpensed"> -->
            <!-- <el-input v-model="form.canBeExpensed" placeholder="请输入可用于费用" /> -->
            <!-- <el-radio v-model="form.canBeExpensed" label="t">是</el-radio>
            <el-radio v-model="form.canBeExpensed" label="f">否</el-radio>
          </el-form-item> -->
         
          <!-- <el-form-item label="Active" prop="active">
            <el-input v-model="form.active" placeholder="请输入Active" />
          </el-form-item> -->
          <!-- <el-form-item label="颜色" prop="color">
            <el-input v-model="form.color" placeholder="请输入颜色" />
          </el-form-item> -->
          <!-- <el-form-item label="创建者id" prop="createUid">
            <el-input v-model="form.createUid" placeholder="请输入创建者id" />
          </el-form-item> -->
          <!-- <el-form-item label="创建日期" prop="createDate">
            <el-date-picker clearable v-model="form.createDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建日期">
            </el-date-picker>
          </el-form-item> -->
          <!-- <el-form-item label="最后更新者id" prop="writeUid">
            <el-input v-model="form.writeUid" placeholder="请输入最后更新者id" />
          </el-form-item> -->
          <!-- <el-form-item label="最后更新日期" prop="writeDate">
            <el-date-picker clearable v-model="form.writeDate" type="date" value-format="yyyy-MM-dd"
              placeholder="请选择最后更新日期">
            </el-date-picker>
          </el-form-item> -->
          
          <!-- <el-form-item label="初始库存" prop="quantyWhenInsert">
            <el-input v-model.number="form.quantyWhenInsert" placeholder="请输入初始库存" />
          </el-form-item> -->
          
          <!-- <el-form-item label="仓库(海内外)" prop="warehouse">
            <el-radio v-model="form.warehouse" label="国内">国内仓</el-radio>
            <el-radio v-model="form.warehouse" label="海外">海外仓</el-radio>
          </el-form-item>
          -->
        <el-form-item label="供应商" prop="partnerId">
          <el-select v-model="form.partnerId" filterable placeholder="请选择供应商" :filter-method="supplierfilterMethod">
                        <el-option v-for="item in supplierdata" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="最低采购数量" prop="minQty">
                    <el-input v-model.number="form.minQty" placeholder="请输入最低采购数量" />
                </el-form-item>
                <el-form-item label="供应商报价" prop="price">
                    <el-input v-model="form.price" placeholder="请输入供应商报价"></el-input>
                </el-form-item>
          <!-- <el-form-item label="供应商" prop="partnerId">
            <el-select v-model="form.partnerId" filterable placeholder="请选择供应商">
              <el-option v-for="item in supplierdata" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最低采购数量" prop="purchaseMinQty">
            <el-input v-model.number="form.purchaseMinQty" placeholder="请输入最低采购数量" />
          </el-form-item>
          <el-form-item label="供应商报价" prop="price">
            <el-input v-model="form.price" placeholder="请输入供应商报价" />
          </el-form-item>  -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listStock, getsupplieradd, delStock, getsupplierdel, updateStock, getOperationdetailsAPI,getSupplierinfo,getreorderadd,getreorderupdate } from "@/api/system/stock";
  import { listPartner, getPartner, delPartner, addPartner, updatePartner, getdetail } from "@/api/system/partner";
  import stockdetail from '@/views/system/stockdetail/index'
  import supplierpricedetail from '@/views/system/supplierpricedetail/index'
  import supplierpriceAdd from '@/views/system/supplierpriceAdd/index'

  

  
  export default {
    name: "Stock",
    dicts: ['product_type', 'product_category'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 库存表格数据
        stockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示操作详情弹出层
        op: false,
        // 数据详情
        tableData: [],
        // 是否显示详情弹出层
        dialogVisible: false,
        // 详情返回值
        result: {},
        // 仓库(海内外)时间范围
        daterangeCreateDate: [],
        // 仓库(海内外)时间范围
        daterangeWriteDate: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          sequence: null,
          description: null,
          descriptionPurchase: null,
          descriptionSale: null,
          type: null,
          categId: null,
          listPrice: null,
          volume: null,
          weight: null,
          saleOk: null,
          purchaseOk: null,
          uomId: null,
          active: null,
          color: null,
          createUid: null,
          createDate: null,
          writeUid: null,
          writeDate: null,
          specification: null,
          canBeExpensed: null,
          warehouse: null,productName:null
        },
        // 表单参数
        form: { },
        // 表单校验
        rules: {
          // id: [
          //   { required: true, message: "id不能为空", trigger: "blur" }
          // ],
          quantyWhenInsert: [{
            required: true, message: "初始库存不能为空", trigger: "blur"
          },
          { type: 'number', message: '库存预警数量只能输入整数', trigger: 'blur' }]
          ,
          saleOk: [
            { required: true, message: "可用于销售不能为空", trigger: "blur" }
          ],
          purchaseOk: [
            { required: true, message: "可用于采购不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          type: [
            { required: true, message: "类型不能为空", trigger: "change" }
          ],
          categId: [
            { required: true, message: "内部类别不能为空", trigger: "change" }
          ],
          price: [{ required: true, message: '供应商报价不能为空' },
          { pattern: /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g, message: '供应商报价只能输入数字', trigger: 'blur' }
  
          ],
          purchaseMinQty: [
            { required: true, message: "最低采购数量不能为空", trigger: "blur" },
            { type: 'number', message: "最低采购数量只能输入数字", trigger: "blur" }
          ],
          partnerId: [
            { required: true, message: "供应商不能为空", trigger: "blur" }
          ],
          productMinQty: [{ required: true, message: '库存预警数量不能为空' },
          { type: 'number', message: '库存预警数量只能输入整数', trigger: 'blur' }
  
          ],
          productMaxQty: [
            { required: true, message: "补充推荐数量不能为空", trigger: "blur" },
            { pattern: /^[+]{0,1}(\d+)$/, message: "补充推荐数量只能输入正整数", trigger: "blur" }
          ],
        },
        // 供应商列表,用于下拉框
        supplierdata: []
      };
    },
    created() {
      this.getList();
      this.supplierList()
    },
    methods: {
      /** 查询库存列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
          this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
          this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
        }
        if (null != this.daterangeWriteDate && '' != this.daterangeWriteDate) {
          this.queryParams.params["beginWriteDate"] = this.daterangeWriteDate[0];
          this.queryParams.params["endWriteDate"] = this.daterangeWriteDate[1];
        }
        listStock(this.queryParams).then(response => {
          this.stockList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
          },
       // 供应商搜索
       supplierfilterMethod(node, val) {

// console.log('val', val, 'node', node)
// console.log(node);
const que = {
    name: node,
    pageSize: 100,
    supplier: 't'
}
// console.log(que);
listPartner(que).then(res => {
    // console.log(res);
    this.supplierdata = res.rows
})
},
      // 取消按钮
      cancel() {
        this.dialogVisible = false;
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          sequence: null,
          description: null,
          descriptionPurchase: null,
          descriptionSale: null,
          type: null,
          categId: null,
          listPrice: null,
          volume: null,
          weight: null,
          saleOk: null,
          purchaseOk: null,
          uomId: null,
          active: null,
          color: null,
          createUid: null,
          createDate: null,
          writeUid: null,
          writeDate: null,
          specification: null,
          canBeExpensed: null,
          warehouse: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeCreateDate = [];
        this.daterangeWriteDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
     
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加库存";
        this.form.type='库存产品'
        
      },
      /** 供应商报价新增或修改操作 */
        handleUpdate(row) {
            const id=row.id
        // console.log(row);
        this.$router.push({
          path: "/supplierpriceAdd",
          component: supplierpriceAdd,
          redirect: "noRedirect",
          hidden: false,
          alwaysShow: true,
          // query: { listId: 10183 },
          query: { listId: id },  
        });
      },
      // 供应商报价详情
      Addprice(row) {
            const id=row.id
        // console.log(row);
        this.$router.push({
          path: "/supplierpricedetail",
          component: supplierpricedetail,
          redirect: "noRedirect",
          hidden: false,
          alwaysShow: true,
          // query: { listId: 10183 },
          query: { listId: id },  
        });
      },
      // 库存详情按钮
      detail(row) {
        const id = row.id
        this.$router.push({
          path: "/stockdetail",
          component: stockdetail,
          redirect: "noRedirect",
          hidden: false,
          alwaysShow: true,
          // query: { listId: 10183 },
          query: { listId: id },
  
        });
        // getStock(id).then(response => {
        //   console.log(response);
        //   // this.form = response.data;
        //   // this.result = response.data
        //   // this.dialogVisible = true;
        //   // this.title = "库存详情";
        // });
      },
      // 操作详情按钮
      Operationdetail(row) {
        const id = row.id;
        this.op = true
        getOperationdetailsAPI(id).then(response => {
          // console.log(response);
          this.tableData = response.data;
  
          this.title = "操作详情";
        });
      },
      // 下拉框选择供应商
      supplierList() {
        listPartner(this.queryParams).then(res => {
          // console.log(res);
          this.supplierdata = res.rows
          // console.log(this.supplierdata);
  
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
              if (valid) {
                // console.log(this.form);
                
                getsupplieradd(this.form).then(response => {
                        this.$modal.msgSuccess("新增成功");
                        this.supplieradd = false;
                        //   this.getList();
                        this.getSupplierinfo(this.form.id)
                    });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除库存编号为"' + ids + '"的数据项？').then(function () {
          return delStock(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/stock/export', {
          ...this.queryParams
        }, `stock_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  